package org.syndicate.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.syndicate.domain.SharePie;

@RooWebScaffold(path = "sharepies", formBackingObject = SharePie.class)
@RequestMapping("/sharepies")
@Controller
public class SharePieController {
	
	@RequestMapping(method = RequestMethod.POST)
    public String create(@Valid SharePie sharePie, BindingResult result, Model model, HttpServletRequest request) {
        if (result.hasErrors()) {
            model.addAttribute("sharePie", sharePie);
            return "sharepies/create";
        }
        //TODO create facade method!!!
        sharePie.create();
        sharePie.persist();
        return "redirect:/sharepies/" + encodeUrlPathSegment(sharePie.getId().toString(), request);
    }
}
